package com.qianfeng.dbsm.impl;

import com.qianfeng.dbsm.dao.TbOrderDao;
import com.qianfeng.dbsm.dao.TbOrderInvoiceDao;
import com.qianfeng.dbsm.entity.TbOrder;
import com.qianfeng.dbsm.entity.TbOrderInvoice;
import com.qianfeng.dbsm.service.TbOrderInvoiceService;
import com.qianfeng.dbsm.vo.CompanyInvoiceVo;
import com.qianfeng.dbsm.vo.PersonalInvoiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * (TbOrderInvoice)表服务实现类
 *
 * @author Chance
 * @since 2022-11-15 15:38:28
 */
@Service("tbOrderInvoiceService")
public class TbOrderInvoiceServiceImpl implements TbOrderInvoiceService {
    @Autowired
    private TbOrderInvoiceDao tbOrderInvoiceDao;

    @Autowired
    private TbOrderDao orderDao;
    /**
     * 个人发票开具
     * @param personalInvoiceVo
     * @return
     */
    @Override
    public int createInvoiceByPersonal(PersonalInvoiceVo personalInvoiceVo) {

        //判断当前是否已经开具发票
        if (isCreatedInvoice(personalInvoiceVo.getOrderNumber())) {
            throw new RuntimeException("当前订单已经开具发票！！！不可重复操作");
        }
        TbOrderInvoice orderInvoice = new TbOrderInvoice();
       try{
           orderInvoice.setCreatedTime(new Date());
           orderInvoice.setOrderNumber(personalInvoiceVo.getOrderNumber());
           orderInvoice.setInvoiceTitle(personalInvoiceVo.getInvoiceTitle());
           orderInvoice.setInvoiceContent(personalInvoiceVo.getInvoiceContent());
           orderInvoice.setInvoiceTaxNo(personalInvoiceVo.getInvoiceTaxNo());
           TbOrder order = orderDao.queryByOrderNumber(personalInvoiceVo.getOrderNumber());
           if(order == null){
               throw new RuntimeException("订单编号不存在！请输入正确的订单编号！！！");
           }
           orderInvoice.setInvoiceAmount(order.getPayPrice());//金额
           orderInvoice.setInvoiceTax(order.getPayPrice()*0.13);//开税金
           orderInvoice.setIsVat(1);
       }catch (Exception e){
           throw new RuntimeException("开具发票失败，请联系管理员");
       }
        int line = 0;
        line = tbOrderInvoiceDao.insert(orderInvoice);
        return line;
    }
    /**
     * 公司发票开具
     * @param companyInvoiceVo
     * @return
     */
    @Override
    public int createInvoiceByCompany(CompanyInvoiceVo companyInvoiceVo) {

        //判断当前是否已经开具发票
        if (isCreatedInvoice(companyInvoiceVo.getOrderNumber())) {
            throw new RuntimeException("当前订单已经开具发票！！！不可重复操作");
        }
        TbOrderInvoice orderInvoice = new TbOrderInvoice();
        try{
          orderInvoice.setCreatedTime(new Date());
          TbOrder order = orderDao.queryByOrderNumber(companyInvoiceVo.getOrderNumber());
            if(order == null){
                throw new RuntimeException("订单编号不存在！请输入正确的订单编号！！！");
            }
          orderInvoice.setOrderNumber(companyInvoiceVo.getOrderNumber());
          orderInvoice.setVatCompanyName(companyInvoiceVo.getVatCompanyName());
          orderInvoice.setVatCompanyAddress(companyInvoiceVo.getVatCompanyAddress());
          orderInvoice.setVatTelphone(companyInvoiceVo.getVatTelphone());
          orderInvoice.setVatBankName(companyInvoiceVo.getVatBankName());
          orderInvoice.setVatBankAccount(companyInvoiceVo.getVatBankAccount());
          orderInvoice.setInvoiceAmount(order.getPayPrice());//金额
          orderInvoice.setInvoiceTax(order.getPayPrice()*0.13);//开税金
          orderInvoice.setIsVat(2);
      }catch (Exception e){
          throw new RuntimeException("开具发票失败:"+e.getMessage());
      }
        int line = 0;
        line = tbOrderInvoiceDao.insert(orderInvoice);
        return line;
    }

    /**
     * 判断当前订单编号是否已经开具发票
     * @param orderNumber
     * @return
     */
    private boolean isCreatedInvoice(String orderNumber){
        TbOrderInvoice orderInvoice = tbOrderInvoiceDao.queryByOrderNumber(orderNumber);
        if(orderInvoice != null){
            return true;
        }
        return false;
    }
}
